<?php
//1. CONNECTS TO THE DATABASE via CREDENTIALS DEFINED IN THE FOLLOWING VARIABLES

$host = 'localhost';
//DEFINES DATABASE TO CONNECT TO
$db = 'pepgsou_main';
//DEFINES WHICH USER TO CONNECT AS ON DATABASE
$user = 'pepgsou_admin';
//DEFINES THE PASSWORD OF THIS USER
$pass = '_20pepg09';
$con=mysql_connect($host,$user,$pass);
	if (!$con){
		die('Could not connect: ' . mysql_error());
		}
	else {
		mysql_select_db($db) or die("Unable to select database");}

//POST VARIABLES
$English_9 = $_POST['English'];
$SocialSciences_9 = $_POST['SocialSciences'];
$Science_9 = $_POST['Science'];
$Mathematics_9 = $_POST['Mathematics'];
$PE_Health_Sem1_9 = $_POST['PE_Health_Sem1'];
$PE_Health_Sem2_9 = $_POST['PE_Health_Sem2'];
$Elective1Sem1_9 = $_POST['Elective1Sem1'];
$Elective2Sem1_9 = $_POST['Elective2Sem1'];
$Elective1Sem2_9 = $_POST['Elective1Sem2'];
$Elective2Sem2_9 = $_POST['Elective2Sem2'];


$English_RowID = 1;
$English_RowID2 = 2;
$SocialSciences_RowID = 3;
$SocialSciences_RowID2 = 4;
$Science_RowID = 5;
$Science_RowID2 = 6;
$Mathematics_RowID = 7;
$Mathematics_RowID2 = 8;
$PE_Health_Sem1_RowID = 9;
$PE_Health_Sem2_RowID = 10;
$Elective1Sem1_RowID = 11;
$Elective2Sem1_RowID = 12;
$Elective1Sem2_RowID = 13;
$Elective2Sem2_RowID = 14;

$query="SELECT CourseID FROM JL WHERE MinGrade=9 AND Title='$English_9'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$English_ID=$row['CourseID'];
//echo $English_ID;


//echo $SocialSciences_9;
$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$SocialSciences_9'";
//echo $query;
$result = mysql_query($query);
$row=mysql_fetch_array($result);
$SocialSciences_ID=$row['CourseID'];
//$SocialSciences_ID = mysql_result($result,1,"CourseID");

$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$Science_9'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
$Science_ID=$row['CourseID'];
//$Science_ID = mysql_result($result,1,"CourseID");

$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$Mathematics_9'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
$Mathematics_ID=$row['CourseID'];
//$Mathematics_ID = mysql_result($result,1,"CourseID");

//$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$PE_Health_Sem1_9'";
//echo $query;
//$result = mysql_query($query);
//$row=mysql_fetch_array($result);
$PE_Health_Sem1_ID=$PE_Health_Sem1_9;
//echo $PE_Health_Sem1_ID;
//$PE_Health_Sem1_ID = mysql_result($result,1,"CourseID");

//$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$PE_Health_Sem2_9'";
//echo $query;
//$result = mysql_query($query);
//$row=mysql_fetch_array($result);
$PE_Health_Sem2_ID=$PE_Health_Sem2_9;
//echo $PE_Health_Sem2_ID;
//$PE_Health_Sem2_ID = mysql_result($result,1,"CourseID");

$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$Elective1Sem1_9'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
$Elective1Sem1_ID=$row['CourseID'];
//$Elective1Sem1_ID = mysql_result($result,1,"CourseID");

$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$Elective2Sem1_9'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
$Elective2Sem1_ID=$row['CourseID'];
//$Elective2Sem1_ID = mysql_result($result,1,"CourseID");

$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$Elective1Sem2_9'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
$Elective1Sem2_ID=$row['CourseID'];
//$Elective1Sem2_ID = mysql_result($result,1,"CourseID");

$query = "SELECT CourseID FROM JL WHERE MinGrade = 9 AND Title ='$Elective2Sem2_9'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
$Elective2Sem2_ID=$row['CourseID'];
//$Elective2Sem2_ID = mysql_result($result,1,"CourseID");

//Get the student's login ID from the login page and then get the year they graduate in

$student_id = $_COOKIE['ID'];
$query = "SELECT graduation_year FROM students WHERE id = '$student_id'";
$year = (mysql_query($query) - 3)+2000;


$query = "Insert INTO course_selection VALUES ('null', '$English_ID', '$student_id', '$year', '1', '$English_RowID')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$English_ID', '$student_id', '$year', '2', '$English_RowID2')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$SocialSciences_ID', '$student_id', '$year', '1', '$SocialSciences_RowID')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$SocialSciences_ID', '$student_id', '$year', '2', '$SocialSciences_RowID2')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Science_ID', '$student_id', '$year', '1', '$Science_RowID')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Science_ID', '$student_id', '$year', '2', '$Science_RowID2')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Mathematics_ID', '$student_id', '$year', '1', '$Mathematics_RowID')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Mathematics_ID', '$student_id', '$year', '2', '$Mathematics_RowID2')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$PE_Health_Sem1_ID', '$student_id', '$year', '1', '$PE_Health_Sem1_RowID')";
//echo $query;
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$PE_Health_Sem2_ID', '$student_id', '$year', '2', '$PE_Health_Sem2_RowID')";
//echo $query;
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Elective1Sem1_ID', '$student_id', '$year', '1', '$Elective1Sem1_RowID')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Elective2Sem1_ID', '$student_id', '$year', '1', '$Elective2Sem1_RowID')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Elective1Sem2_ID', '$student_id', '$year', '2', '$Elective1Sem2_RowID')";
mysql_query($query) or die(mysql_error());


$query = "Insert INTO course_selection VALUES ('null', '$Elective2Sem2_ID', '$student_id', '$year', '2', '$Elective2Sem2_RowID')";
mysql_query($query) or die(mysql_error());
mysql_close();

echo "DONE";

?>